<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/14
 * Time: 14:32
 */
namespace App\Http\Controllers\Manage;

use App\Model\Affix;
use App\Model\Annex;
use Log;
use App\Model\Knowledge;
use App\Model\KnowledgeCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Model\ProductCategory;
use OSS\OssClient;
use OSS\Core\OssException;

class KnowledgeController extends Controller {
    public function knowledgeList() {
        $page = Input::get('page');
        $count = Input::get('count');
        $orderBy = Input::get('orderBy');
        $obj = new Knowledge();
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $knowledge = $obj->Paginate(20);
        foreach($knowledge as $k) {
            $category_id = $k->category;
            $category = KnowledgeCategory::find($category_id);
            if (!empty($category)) {
                $k->category = $category->name;
            }
        }
        if(empty($page)){
            $count = $obj->count();
        }

        return view('manage.knowledge.knowledgeList',[
            'knowledge'=>$knowledge,
            'count'=>$count,
            'orderBy'=>$orderBy,
            'active1' => 12,
            'active2' => 1]);
    }

    public function knowledgeAdd() {
        $knowledge_category = KnowledgeCategory::where('status',1)->get();
        return view('manage.knowledge.knowledgeAdd', ['knowledge_category' => $knowledge_category, 'active1'=>12,'active2'=>2]);
    }

    public function knowledgeCreate() {
        $rank = input::get('rank');
        $title = input::get('name');
        $category = input::get('category');
        $content = input::get('content');
        $dd_id = rand(1,99);
        $label = input::get('label');
        $this->labelCreate($label);
        if(empty($title) || empty($category)) {
            return [
                'code' => 102,
                'msg' => "缺少参数!"
            ];
        }
        $repetition  = Knowledge::where('name',$title)->first();
        if($repetition) {
            return [
                'code' => 103,
                'msg' => '该名称已存在!'
            ];
        }
        $map = Knowledge::insert([
            'title' => $title,
            'rank' => $rank,
            'category' => $category,
            'content' => $content,
        ]);

        if($map) {
            return [
                'code' => 100,
                'msg' => '添加成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'添加失败!'
            ];
        }


    }

    public function knowledgeEdit($id) {
        $knowledge_id = $id;
        $knowledge = Knowledge::find($id);
        $knowledge_category = KnowledgeCategory::where('status',1)->get();
        if (!empty($knowledge)) {
            return view('manage.knowledge.knowledgeEdit', ['knowledge' => $knowledge, 'knowledge_category' => $knowledge_category, 'active1' => 12, 'active2' => 1]);
        }
        else {

        }
    }

    public function knowledgeUpdate() {
        $id = input::get('id');
        $data['rank'] = input::get('rank');
        $data['title'] = input::get('title');
        $data['category'] = input::get('category');
        $data['content'] = input::get('content');
        $data['lable'] = input::get('lable');
        if(empty($data['title']) || empty($data['content'])) {
            return [
                'code' => 102,
                'msg' => '缺少元素!'
            ];
        }
        $update = Knowledge::where('id',$id)->update($data);
        if($update) {
            return [
                'code' => 100,
                'msg' => '修改成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'修改失败!'
            ];
        }
    }

    public function knowledgeDelete(){
        $id = Input::get('id');
        $delete = Knowledge::where('id',$id)->delete();
        if($delete){
            return [
                'code'=>100,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'code'=>101,
                'msg'=>'删除失败！'
            ];
        }
    }

    public function knowledgeStatus() {

        $id = Input::get('id');
        $status = Input::get('status');

        if($status == 1) {
            $del = Knowledge::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = Knowledge::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    private function labelCreate($label) {
        Log::info('label here');
    }
}